<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>菜单管理</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">资源列表</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <div class="layui-row">
                        <div class="layui-form" style="float:right;">
                            <div class="layui-form-item" style="margin:0;">
                                <label class="layui-form-label">资源标题</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="title" id="title" placeholder="资源标题" autocomplete="off" class="layui-input">
                                </div>
                                <label class="layui-form-label">所属系统</label>
                                <div class="layui-input-inline">
                                    <select name="systemType" id="systemType" lay-search="">
                                        <option value="">请选择所属系统</option>
                                        <option value="1">运营平台</option>
                                        <option value="2">商户系统</option>
                                        <option value="3">代理商系统</option>
                                    </select>
                                </div>
                                <button id="search" class="layui-btn" data-type="reload">
                                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                    <div class="layui-card-body">
                        <div style="padding-bottom: 10px;">
                            <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
                            <button class="layui-btn" data-type="del">删除</button>
                        </div>
                    </div>
                    <table id="Pay_Mgr_Resource_dataAll" lay-filter="Pay_Mgr_Resource_dataAll"></table>
                </div>

            </div>
        </div>
    </div>
</div>

<script type="text/html" id="payBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="add">添加子资源</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    layui.use(['admin', 'table', 'util'], function () {
        var $ = layui.$
            , admin = layui.admin
            , table = layui.table
            , element = layui.element,
            view = layui.view
            , layer = layui.layer
            , form = layui.form;

        element.render('breadcrumb', 'breadcrumb');

        var tplSystemType = function (d) {
            if (d.systemType == 1) {
                return "<span style='color: green'>运营平台</span>";
            } else if (d.systemType == 2) {
                return "<span style='color: blue'>商户系统</span>";
            } else if (d.systemType == 3) {
                return "<span style='color: blue'>代理商系统</span>";
            }
        };

        // 资源列表
        table.render({
            elem: '#Pay_Mgr_Resource_dataAll'
            , url: layui.setter.baseUrl + '/sys/resource/list' //列表接口
            , where: {
                access_token: layui.data(layui.setter.tableName).access_token
            }
            , id: 'tableReload'
            , page: true
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'resourceId', title: '资源ID'}
                , {field: 'name', title: '资源名称'}
                , {field: 'title', title: '资源标题'}
                , {field: 'permName', title: '授权名'}
                , {field: 'permUrl', title: '授权URL'}
                , {field: 'system', title: '所属系统', templet: tplSystemType}
                , {field: 'edit', width: 260, title: '操作', toolbar: '#payBar'}
            ]]
            , skin: 'line'
        });

        // 监听工具条
        table.on('tool(Pay_Mgr_Resource_dataAll)', function (obj) {
            var data = obj.data;
            var resourceId = data.resourceId;

            if (obj.event === 'detail') {
                var resourceId = data.resourceId;
                location.href = layui.setter.baseLocal + "sys/resource/resource_view/resourceId=" + resourceId;
            } else if (obj.event === 'del') {
                layer.confirm('确认删除么', function (index) {
                    admin.req({
                        url: layui.setter.baseUrl + '/sys/resource/delete',
                        data: {
                            resourceIds: data.resourceId
                        },
                        success: function () {
                            layer.msg('删除成功', {
                                icon: 1
                            });
                            table.reload("tableReload");
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                location.href = layui.setter.baseLocal + "sys/resource/resource_edit/resourceId=" + data.resourceId;
            } else if (obj.event === 'add') {
                location.href = layui.setter.baseLocal +  "sys/resource/resource_add/parentId=" + data.resourceId;
            }
        });

        // 搜索
        var $ = layui.$, active = {
            reload: function () {
                var title = $("#title").val();
                var systemType = $("#systemType").val();
                //执行重载
                table.reload('tableReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        systemType: systemType,
                        title: title
                    }
                });
            }

            , add: function (othis) {
                var checkStatus = table.checkStatus("tableReload")
                    , data = checkStatus.data; // 获得选中的数据
                if (data.length > 1) {
                    layer.msg('只能给一个父资源添加子资源');
                    return false;
                }
                var resourceId = "";
                if (data[0] != undefined) {
                    resourceId = data[0].resourceId;
                }
                location.href = layui.setter.baseLocal + "sys/resource/resource_add/parentId=" + resourceId;
            }

            , del: function (othis) {
                var checkStatus = table.checkStatus("tableReload")
                    , data = checkStatus.data; //获得选中的数据
                if (data.length === 0) return layer.msg('请选择数据');
                console.log(data);
                layer.confirm('确定删除选中的数据吗？', function () {
                    var resourceIds = "";
                    $.each(data, function (i, n) {
                        resourceIds = resourceIds + "," + n.resourceId;
                    });
                    console.log("resourceIds");
                    if (resourceIds != "") {
                        resourceIds = resourceIds.substring(1);
                        admin.req({
                            url: layui.setter.baseUrl + '/sys/resource/delete',
                            data: {
                                resourceIds: resourceIds
                            },
                            success: function () {
                                layer.msg('删除成功', {
                                    icon: 1
                                });
                                table.reload("tableReload");
                            }
                        });
                    }
                });
            }
        };
        $('#search').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $('.layui-btn.layuiadmin-btn-list').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $('.layuiAdmin-btns .layui-btn').on('click', function () {
            var othis = $(this)
                , thisEvent = othis.data('events')
                , type = othis.data('type');
            events[thisEvent] && events[thisEvent].call(this, othis, type);
        });
        $('.layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        // 渲染表单
        form.render();
    });
</script>